package org.bambrikii.examples.rutext.lexer.custom;

import java.util.Map;

import org.bambrikii.examples.rutext.dictionary.LongDictionaryLoader;
import org.bambrikii.examples.rutext.lexer.CaseValue;
import org.bambrikii.examples.rutext.lexer.impl.LexerBase;
import org.bambrikii.examples.rutext.parser.IParser;
import org.bambrikii.examples.rutext.parser.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IntegerLexer extends LexerBase<Long> {

	private static final Logger logger = LoggerFactory.getLogger(IntegerLexer.class);

	private static Map<String, Long> DICT = null;
	private static Map<String, CaseValue> DICT_CASES = null;

	static {
		LongDictionaryLoader loader = new LongDictionaryLoader();
		DICT = loader.load("org.bambrikii.examples.rutext.IntegerLexer", "NUMBERS");
		DICT_CASES = loader.loadCases("org.bambrikii.examples.rutext.IntegerLexer", "NUMBERS__CASES");
	}

	public static Long findValue(String val) {
		if (DICT_CASES.containsKey(val)) {
			return DICT.get(DICT_CASES.get(val).getKey());
		}
		if (DICT.containsKey(val)) {
			return DICT.get(val);
		}
		return null;
	}

	public IntegerLexer(IParser parser) {
		super(parser);
	}

	@Override
	public Long lex() {
		try {
			if (hasNext()) {
				String val = getLast();
				Long val2 = findValue(val);
				if (val2 != null) {
					logger.debug("success [" + val + "]");
					consume();
					return val2;
				}
			}
		} catch (ParserException e) {
			throw new RuntimeException(e);
		}
		return null;
	}

}
